﻿using System.Windows.Media;

namespace DotNetUI.Models
{
    /// <summary>
    /// 内部颜色
    /// </summary>
    public class InnerColor : ModelBase
    {
        /// <summary>
        /// 原始颜色
        /// </summary>
        public Color SourceColor
        {
            get => sourceColor; set
            {
                sourceColor = value;
                RaisedPropertyChanged();
                RaisedPropertyChanged(nameof(A));
                RaisedPropertyChanged(nameof(R));
                RaisedPropertyChanged(nameof(G));
                RaisedPropertyChanged(nameof(B));
                RaisedPropertyChanged(nameof(Hue));
                RaisedPropertyChanged(nameof(Saturation));
                RaisedPropertyChanged(nameof(Brightness));
            }
        }
        private byte a;
        private byte r;
        private byte g;
        private byte b;

        /// <summary>
        /// 内部颜色
        /// </summary>
        /// <param name="color"></param>
        public InnerColor(Color color)
        {
            A = color.A;
            R = color.R;
            G = color.G;
            B = color.B;

            this.SourceColor = color;
            var hsb = ColorHelper.Rgb2Hsb(color);
            Hue = hsb.Hue;
            Brightness = hsb.Brightness;
            Saturation = hsb.Saturation;
        }
        /// <summary>
        /// 透明度
        /// </summary>

        public byte A
        {
            get => a; set
            {
                a = value;
                SourceColor = Color.FromArgb(value, R, G, B);
                RaisedPropertyChanged();
            }
        }
        /// <summary>
        /// 红色
        /// </summary>
        public byte R
        {
            get => r; set
            {
                r = value;
                SourceColor = Color.FromArgb(A, R, G, B);

                RaisedPropertyChanged();

            }
        }
        /// <summary>
        /// 绿色
        /// </summary>
        public byte G
        {
            get => g; set
            {
                g = value;
                RaisedPropertyChanged();
                SourceColor = Color.FromArgb(A, R, G, B);

            }
        }
        /// <summary>
        /// 蓝色
        /// </summary>
        public byte B
        {
            get => b; set
            {
                b = value;
                RaisedPropertyChanged();
                SourceColor = Color.FromArgb(A, R, G, B);

            }
        }
        private double hue;
        private float saturation;
        private float brightness;
        private Color sourceColor;

        /// <summary>
        /// 色相
        /// </summary>
        public double Hue
        {
            get => hue; set
            {
                value = value % 360;
                hue = value;
                var c = new Hsb { Hue = hue, Brightness = Brightness, Saturation = Saturation };
                this.SourceColor = ColorHelper.Hsb2Rgb(c);
                RaisedPropertyChanged();

            }
        }
        /// <summary>
        /// 饱和度
        /// </summary>
        public float Saturation
        {
            get => saturation; set
            {
                saturation = value;
                var c = new Hsb { Hue = hue, Brightness = Brightness, Saturation = Saturation };
                this.SourceColor = ColorHelper.Hsb2Rgb(c);

                RaisedPropertyChanged();

            }
        }
        /// <summary>
        /// 亮度
        /// </summary>
        public float Brightness
        {
            get => brightness; set
            {
                brightness = value;
                var c = new Hsb { Hue = hue, Brightness = Brightness, Saturation = Saturation };
                this.SourceColor = ColorHelper.Hsb2Rgb(c);

                RaisedPropertyChanged();

            }
        }
    }
}
